/*
 * FreeRTOS Kernel V10.0.0
 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software. If you wish to use our Amazon
 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://www.FreeRTOS.org
 * http://aws.amazon.com/freertos
 *
 * 1 tab == 4 spaces!
 */

#include <stdio.h>

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "semphr.h"

/* Hardware and starter kit includes. */
#include "nuc980.h"
#include "sys.h"

extern void vPortYieldProcessor(void);

/* Priorities for the demo application tasks. */
#define TASK1_PRIORITY             ( tskIDLE_PRIORITY + 3UL )
#define TASK2_PRIORITY             ( tskIDLE_PRIORITY + 4UL )

#define TASK_STACK_SIZE           ( configMINIMAL_STACK_SIZE )
/*
 * Set up the hardware ready to run this demo.
 */
static void prvSetupHardware( void )
{

    sysDisableCache();
    sysFlushCache(I_D_CACHE);
    sysEnableCache(CACHE_WRITE_BACK);

    /* enable UART0 clock */
    outpw(REG_CLK_PCLKEN0, inpw(REG_CLK_PCLKEN0) | 0x10000);

    /* GPF11, GPF12 */
    outpw(REG_SYS_GPF_MFPH, (inpw(REG_SYS_GPF_MFPH) & 0xfff00fff) | 0x11000);   // UART0 multi-function

    /* UART0 line configuration for (115200,n,8,1) */
    outpw(REG_UART0_LCR, inpw(REG_UART0_LCR) | 0x07);
    outpw(REG_UART0_BAUD, 0x30000066); /* 12MHz reference clock input, 115200 */

    sysSetLocalInterrupt(ENABLE_IRQ);
}
/*-----------------------------------------------------------*/


static void Task_1( void *pvParameters )
{
    for( ;; )
    {
        printf("Check Task1 is running ...\n");
        vTaskDelay(800);
    }
}

static void Task_2( void *pvParameters )
{
    for( ;; )
    {
        printf("Check Task2 is running ...\n");
        vTaskDelay(500);
    }
}

int task_main(void)
{

    sysInstallExceptionHandler(SYS_SWI, vPortYieldProcessor);
    /* Configure the hardware ready to run the test. */
    prvSetupHardware();

    xTaskCreate( Task_1, "Task_1", TASK_STACK_SIZE, NULL, TASK1_PRIORITY, NULL );
    xTaskCreate( Task_2, "Task_2", TASK_STACK_SIZE, NULL, TASK2_PRIORITY, NULL );

    printf("FreeRTOS is starting ...\n");

    /* Start the scheduler. */
    vTaskStartScheduler();

    /* If all is well, the scheduler will now be running, and the following line
    will never be reached.  If the following line does execute, then there was
    insufficient FreeRTOS heap memory available for the idle and/or timer tasks
    to be created.  See the memory management section on the FreeRTOS web site
    for more details. */
    for( ;; );
}




